<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Gift extends Model
{
    protected $table = 'gift';
    
    protected $fillable = [
        'name',
        'description',
        'points_required',
        'stock',
        'status',
        'start_time',
        'end_time',
        'sort_order',
        'need_shipping',
    ];

    protected $casts = [
        'points_required' => 'integer',
        'stock' => 'integer',
        'status' => 'integer',
        'sort_order' => 'integer',
        'need_shipping' => 'boolean',
        'start_time' => 'datetime',
        'end_time' => 'datetime',
        'create_time' => 'datetime',
        'update_time' => 'datetime',
    ];

    // 常量定义
    const STATUS_OFFLINE = 0;
    const STATUS_ONLINE = 1;

    // 指定时间戳字段名
    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';


    // 判断礼品是否可兑换
    public function isAvailable()
    {
        if ($this->status !== self::STATUS_ONLINE) {
            return false;
        }

        if ($this->stock === 0) {
            return false;
        }

        $now = now();
        if ($now < $this->start_time) {
            return false;
        }

        if ($this->end_time && $now > $this->end_time) {
            return false;
        }

        return true;
    }
} 